home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu219.dms / pu219.adf / SOURCES / Eaters.c < prev    next >
C/C++ Source or Header  |  1992-07-16  |  5KB  |  175 lines

  1. /* Eaters by Guido Wegener of CLUSTER */
  2. #include "math.h"
  3. #include "exec/types.h"
  4. #include "functions.h"
  5. #include "intuition/intuition.h"
  6. #include "graphics/gfxbase.h"
  7. #include "graphics/gfx.h"
  8. #include "stdio.h"
  9. #include "exec/memory.h"
  10. #define ABS(a) ((a<0) ? -a : a)  /* Usefull function, but it didn't work always */
  11. #define LI (long int)   /* The average guru is caused by short variables
  12.                            passed to system functions. */
  13. #define MAXANZ 500
  14.  
  15. struct IntuitionBase *IntuitionBase;
  16. struct GfxBase *GfxBase;
  17. struct Screen wbs;    /* workbenchscreen */
  18. struct ViewPort *vpo; /* wbs' ViewPort  */
  19. struct RastPort *rpo; /* and RastPort   */
  20. struct Window *MyWindow=NULL;
  21. struct NewWindow MyNewWindow=
  22.  {
  23.   0,0,
  24.   391,10,
  25.   -1,-1,
  26.   CLOSEWINDOW,
  27.   NOCAREREFRESH+SMART_REFRESH+WINDOWDEPTH+WINDOWCLOSE+WINDOWDRAG,
  28.   NULL,NULL,
  29.   (UBYTE *)"Eaters by G.Wegener of CLUSTER",
  30.   NULL,NULL,
  31.   391,10,
  32.   391,10,
  33.   WBENCHSCREEN
  34.  };
  35. BOOL closed=FALSE;
  36. int x[MAXANZ],y[MAXANZ],u[MAXANZ];  /* Positions of Eaters; what is beneath them */
  37. int w,h;    /* wbs width, height */
  38. int dcoords[8][2]=  /* double list of where to look for white dots */
  39.  {                  /* as pairs of delta-x,y */
  40.   1,0,
  41.   -1,0,
  42.   0,1,
  43.   0,-1,
  44.   1,0,   /* Once again the same list, this copy is needed to get */
  45.   -1,0,  /* a bit random later */
  46.   0,1,
  47.   0,-1
  48.  };
  49. int anz=35;   /* number of eaters */
  50.      
  51. main(argc,argv)
  52. int argc;
  53. char *argv[];
  54.  {
  55.   struct IntuiMessage *message;
  56.   int i; 
  57.  
  58.   if(argc!=2) info();
  59.   else anz=atoi(argv[1]);
  60.   if(anz<1 || anz>500) anz=35;
  61.   openit();  /* some start values and opens */
  62.  
  63.   while(!closed) /* while not closed ... */
  64.    {
  65.     message=(struct IntuiMessage *)GetMsg(MyWindow->UserPort);
  66.     if(message) /* if there was a message */
  67.      {
  68.       switch(message->Class)
  69.        {
  70.         case CLOSEWINDOW:closed=TRUE;break;
  71.         default : puts("Unknown message class !"); /* You should never see this */
  72.        }
  73.       ReplyMsg((struct Message *)message);
  74.      }
  75.     for(i=0;i<anz;i++) moveme(i);  /* move the Eaters */
  76.    }
  77.   go(NULL); /* very clean exit (Who needs the 23KB that get lost each call ?) */
  78.  }
  79.  
  80. info()
  81.  {
  82.   puts("Eaters V1.0 by Guido Wegener of CLUSTER 21.5.1990");
  83.   puts("Usage : run eaters <n>      where <n> is the number of eaters");
  84.   puts(" n must be smaller than 500, but you won't like it anyway");
  85.  }
  86.  
  87. moveme(n)
  88. int n;
  89.  {
  90.   int i;
  91.   int nx,ny; /* new position of Eater */
  92.   int fc;
  93.  
  94.   fc=RangeRand(4L);    /* starting point in delta-list */
  95.   if(u[n]==1) u[n]=2;  /* turn white into black */
  96.   if(u[n]==3) u[n]=0;  /*  and orange into blue */
  97.   SetAPen(rpo,LI(u[n]));             /* kill the */
  98.   WritePixel(rpo,LI(x[n]),LI(y[n]));        /* image of the Eater */
  99.   nx=x[n];ny=y[n];  /* set default : no move */
  100.   for(i=0;i<4;i++)  /* search for WHITE */
  101.    {
  102.     nx=x[n]+dcoords[fc+i][0];
  103.     if(nx<0 || nx>=w) continue; /* bound-checking */
  104.     ny=y[n]+dcoords[fc+i][1];
  105.     if(ny<0 || ny>=h) continue;
  106.     if(ReadPixel(rpo,LI(nx),LI(ny))==1) break; /* look at neighbour */
  107.    }
  108.   if(nx==x[n] && ny==y[n]) /* if no WHITE is found ... */
  109.    {
  110.     x[n]+=1-RangeRand(3L); /* ... move randomly */
  111.     y[n]+=1-RangeRand(3L);
  112.    }
  113.   else    /* else go to WHITE */
  114.    {
  115.     x[n]=nx;
  116.     y[n]=ny;
  117.    }
  118.   if(x[n]>=w) x[n]=w-1; /* check if Eater is leaving the screen */
  119.   if(x[n]<0) x[n]=0;
  120.   if(y[n]>=h) y[n]=h-1;
  121.   if(y[n]<0) y[n]=0;
  122.   u[n]=ReadPixel(rpo,LI(x[n]),LI(y[n])); /* what color has the new dot ? */
  123.   SetAPen(rpo,3L);
  124.   WritePixel(rpo,LI(x[n]),LI(y[n]));  /* draw eater */
  125.  }
  126.  
  127. openit()
  128.  {
  129.   BOOL succ;
  130.   int i;
  131.   
  132.   IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  133.   if(!IntuitionBase) go("Can't open Intuition !");
  134.   GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L);
  135.   if(!GfxBase) go("Can't open Graphics !");
  136.  
  137.   MyWindow=OpenWindow(&MyNewWindow);
  138.   succ=GetScreenData((CPTR)&wbs,(long int)sizeof(struct Screen),WBENCHSCREEN,NULL);  
  139.   if(!succ) go("Can't find WBENCHSCREEN !"); /* ^ search workbenchscreen */
  140.   vpo=&(wbs.ViewPort); /* WBscreen viewport */
  141.   rpo=&(wbs.RastPort);
  142.   w=wbs.Width;
  143.   h=wbs.Height;
  144.   for(i=0;i<anz;i++) /* initialize positions */
  145.    {
  146.     x[i]=RangeRand(LI(w));
  147.     y[i]=RangeRand(LI(h));
  148.    }
  149.  }
  150.  
  151. go(text)    /* to use a function like this one is VERY usefull */
  152. char *text;
  153.  {
  154.   int i;
  155.   
  156.   if(MyWindow)
  157.     for(i=0;i<anz;i++)
  158.      {
  159.       SetAPen(rpo,LI(u[i]));
  160.       WritePixel(rpo,LI(x[i]),LI(y[i]));
  161.      }
  162.   if(text) puts(text); /* tell about errror */
  163.   
  164.   if(MyWindow) CloseWindow(MyWindow);
  165.   if(GfxBase) CloseLibrary(GfxBase);
  166.   if(IntuitionBase) CloseLibrary(IntuitionBase);
  167.   exit(0);
  168.  }
  169.  
  170. /* :-) Always looking for fun (-:
  171.    . .       CLUSTER          · ·               
  172.     ª          °ª°             ª   
  173. */
  174.  
  175.